package com.isoft.monitor;

import com.isoft.listener.FileAlterationalListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

@Slf4j
@Component
public class FileMonitor {
    //xml文件所在的路径
    private String path="D:\\AIRPORT_XML";
    private long interval=30000;
    @Autowired
    FileAlterationalListener fileAlterationalListener;

    public void moniter(){
        //定义一个监听者
        FileAlterationObserver observer=new FileAlterationObserver(new File(path));
        //为监听者捆绑监听器
        observer.addListener(fileAlterationalListener);
        //建立监听器线程
        FileAlterationMonitor fileAlterationMonitor=new FileAlterationMonitor(interval,observer);
        //开启监听线程
        try {
            fileAlterationMonitor.start();
        } catch (Exception e) {
            log.error("监听线程开启失败");
            e.printStackTrace();
        }
    }
}
